<?php

class Hydrum_Application_Resource_DatabaseConnections extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        $connections = array();

        foreach ($this->getOptions() as $index => $options) {
            
            $connection = $this->createConnection($options);
            
            $connections[$index] = $connection;
        }
        
        return $connections;
    }
    
    protected function createConnection(array $options)
    {
        $type = $options['type'];
        $method = 'create' . $type . 'Connection';
        $connection = $this->$method($options);    
        
        return $connection;
    }
    
    /**
     * Create Mongo DB connection instance
     * 
     * @param array $options
     * @return Doctrine\ODM\MongoDB\Mongo
     */
    protected function createMongoDBConnection(array $options)
    {
        $server = isset($options['server'])? $options['server']: null;
        $params = isset($options['params'])? $options['params']: array();
        
        $connection = new Doctrine\ODM\MongoDB\Mongo($server, $params);  
        
        if (isset($options['db'])) {
            
            $connection->selectDB($options['db']);
        }  
    
        return $connection;
    }
    
    /**
     * Create PDO connection instance
     * 
     * @param array $options
     * @return Doctrine\DBAL\Connection
     */
    protected function createPDOConnection(array $options)
    {
        $connection = Doctrine\DBAL\DriverManager::getConnection($options);
        
        return $connection;
    }
}
